package zr.component;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import zr.Main;
import zr.dao.Player;
import zr.util.Logger;

public class ServerSearchDialog extends JFrame {

	private final static org.apache.log4j.Logger logger = Logger
			.getInstance(ServerSearchDialog.class);
	String userid = "";
	JTextField serverIP = new JTextField("127.0.0.1");
	JButton connect = new JButton("连接");

	public ServerSearchDialog() throws UnknownHostException {
		super();

		this.add(new JLabel("服务器IP:"), BorderLayout.WEST);
		this.add(serverIP, BorderLayout.CENTER);
		this.add(connect, BorderLayout.EAST);

		int width = 300, height = 80;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(width, height);
		this.setLocation((int) (screenSize.getWidth() - width) / 2,
				(int) (screenSize.getHeight() - height) / 2);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		addListeners();

		while (userid == null || userid.length() == 0) {
			userid = JOptionPane.showInputDialog(this, "请输入您的名字", "玩家名字",
					JOptionPane.QUESTION_MESSAGE);
			if (userid != null)
				userid = userid.trim();
		}
		Main.me = new Player(userid);
		Main.me.setIp(InetAddress.getLocalHost().getHostAddress());
	}

	private void addListeners() {
		connect.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {

					Socket socket = new Socket(serverIP.getText(),
							Main.SERVER_PORT);
					DataInputStream input;
					DataOutputStream output;
					input = new DataInputStream(socket.getInputStream());
					output = new DataOutputStream(socket.getOutputStream());
					output.writeUTF(userid);
					output
							.writeUTF(InetAddress.getLocalHost()
									.getHostAddress());

					String room=input.readUTF();
					String hename = input.readUTF();
					String heip = input.readUTF();
					Main.frame.setTitle(room);
					Main.he = new Player(hename);
					Main.he.setIp(heip);

					String buju = input.readUTF();
					System.out.println("接收到布局：" + buju);
					ServerSearchDialog.this.dispose();
					Main.start();
					Main.layout(buju);
					Main.addHisInfoPane();
					Main.myTurn = false;
					Main.startClockThread();

					Main.output = output;
					Main.input = input;
				} catch (UnknownHostException e1) {
					JOptionPane.showMessageDialog(ServerSearchDialog.this,
							"未知的主机" + serverIP.getText());
				} catch (IOException e1) {
					logger.error("尝试连接到服务器时发生异常:" + e1);
					e1.printStackTrace();
				}
				Thread t = new Thread() {
					public void run() {
						try {
							Main.waitCommand();
						} catch (IOException e) {
							e.printStackTrace();
						}

					}
				};
				t.start();
			}
		});
	}
}
